<%@page import="java.sql.*"%>
<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>根据首字查成语</title>
<style type="text/css">
h1{ color:red; }
input, button { padding:10px; border-radius: 6px; outline: none; }
</style>
</head>
<body><% 
String first = request.getParameter("firstWord");
String url="jdbc:mysql://localhost:3306/db?serverTimezone=PRC&allowMultiQueries=true";
//(1)加载驱动
Class.forName("com.mysql.cj.jdbc.Driver");
//(2)创建数据库连接
Connection cn = DriverManager.getConnection(url,"root","12qwas");//  "+first+"
//(3)创建预处理语句对象以执行sql指令
String sql="select cy from chengyu where first=?";
PreparedStatement ps = cn.prepareStatement(sql);
ps.setString(1, first);
%>
<h1>根据首字查成语</h1>
<%= "<h3>"+sql+"</h3>" %>
<p>以【<%=first %>】开头的成语列表：</p>
<%
ResultSet rs = ps.executeQuery();
while(rs.next()){
	String cy = rs.getString("cy");
	out.print(cy+" ");
}
rs.close();
ps.close();
cn.close();
%>
</body>
</html>